package com.example.service.schedule;//package com.example.service.schedule;

import com.example.entity.SystemData;
import com.example.service.BlogService;
import com.example.service.SystemDataService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者：风中叶
 * 功能：定时任务，处理新增博客数据
 */

@Component
public class ScheduleTasks {

//    @Scheduled(cron = "0/3 * * * * *")
//    public void test() {
//        System.out.println(new Date() + "定时任务执行了");
//
//    }

    @Resource
    private BlogService blogService;

    @Resource
    private SystemDataService systemDataService;

    // 在每天04时0分0秒，系统自动统计昨天凌晨4点至今天凌晨4点新增博客数据
    @Scheduled(cron = "0 0 4 * * *")
    public void updateSystemData() {
        Date currentDate = new Date();

        // 定义格式 "MM月dd日"（M=月份，d=日期）
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        String formattedDate = sdf.format(currentDate);

        // 获取当前博客总数
        Long total = blogService.getBlogTotal();
        // 获取前一天的数据
        SystemData systemData = systemDataService.getLastData();
        // 新增博客数量
        Long addBlogNumber = total - systemData.getBlogTotal();
        SystemData newSystemData = new SystemData();
        newSystemData.setTime(formattedDate);
        newSystemData.setAddBlogNumber(addBlogNumber);
        newSystemData.setBlogTotal(total);
        newSystemData.setIs_delete(false);

        systemDataService.addData(newSystemData);
    }

}
